// pages/ucenter/order/order.js
import Dialog from '../../../lib/vant-weapp/dialog/dialog';
import Toast from '../../../lib/vant-weapp/toast/toast';
const jeegot = require('../../../utils/jeegot.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    loading: false,
    loaded: false,
    page: 1,
    totalPage:0,
    orderList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '订单中心',
    })
    // 获取参数
    let type = options.type || 0;
    this.setData({
      active: type
    })
    // 模拟加载数据
    this.loadData(type)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  loadData: function (type) {
    let that = this;
    jeegot.getOrderList({page: that.data.page, status: type}).then(data =>{
      that.setData({
        totalPage: data.pages,
        orderList: that.data.orderList.concat(data.records)
      })
    })
  },
  changeTab: function (e) {
    let type = e.detail.index;
    // 模拟加载数据
    this.setData({
      page: 1,
      active: type,
      orderList: []
    })
    this.loadData(type)
  },
  scrollListen: function (e) {
    if(this.data.page<=this.data.totalPage) {
      let cpage = this.data.page;
      this.setData({page: cpage + 1})
      this.loadData(this.data.active);
    }else{
      this.setData({loaded: true})
      // Toast("没有更多订单了")
    }
  },
  cancelOrder: function (e) {
    Dialog.confirm({
      message: '要取消此订单？'
    }).then(() => {
      // on confirm
      let index = e.currentTarget.dataset.index;
      let orderId = e.currentTarget.dataset.value.id;
      jeegot.cancelOrder({orderId: orderId}).then(data => {
        let dataList = this.data.orderList;
        dataList.splice(index, 1);
        this.setData({orderList: dataList})
      }).catch(error =>{
      })
    }).catch(() => {
      // on cancel
    });
  },

  deleteOrder: function (e) {
    Dialog.confirm({
      message: '要删除此订单？'
    }).then(() => {
      // on confirm
      let id = e.currentTarget.dataset.value.id;
      let data = [];
      this.data.orderList.forEach(function (v) {
        if (id != v.id) {
          data.push(v);
        }
      })
      this.setData({
        orderList: data
      })
    }).catch(() => {
      // on cancel
    });
  },
  confirmReceive: function (e) {
    Dialog.confirm({
      message: '确收到货物？'
    }).then(() => {
      // on confirm
      let index = e.currentTarget.dataset.index;
      let orderId = e.currentTarget.dataset.value.id;
      jeegot.confirmDelivery({orderId: orderId}).then(data =>{
        let dataList = this.data.orderList;
        dataList.splice(index, 1);
        this.setData({orderList: dataList})
        Toast("确认收货成功")
      }).catch((error) => {
        // on cancel
        Toast(error.data)
      })
    });
  },
  toPay: function (e) {
    let actualPrice = e.currentTarget.dataset.value.payAmount;
    let id = e.currentTarget.dataset.value.id;
    jeegot.payOrder({orderId: id}).then(resp =>{
      let wxPay = resp;
      wx.requestPayment({
        'timeStamp': String(wxPay.timeStamp),
        'nonceStr': wxPay.nonceStr,
        'package': wxPay.package,
        'signType': wxPay.signType,
        'paySign': wxPay.paySign,
        'success': function (res) {
          wx.redirectTo({ url: '/pages/pay-result/pay-result?status=1&actualPrice=' + actualPrice})
        },
        'fail': function (res) {
          wx.redirectTo({ url: '/pages/pay-result/pay-result?status=0&actualPrice=' + actualPrice})
        }
      })
    }).catch(error => {
      
    })
  },
  toOrderDetail: function (v) {
    let data = v.currentTarget.dataset.value;
    wx.navigateTo({
      url: '/pages/ucenter/order-detail/order-detail?id='+data.id
    })
  },
  toComment: function (v) {
    let data = v.currentTarget.dataset.value;
    wx.navigateTo({
      url: '/pages/ucenter/to-comment/to-comment?id='+data.id
    })
  },
  toExpress: function (v) {
    wx.navigateTo({
      url: '/pages/ucenter/express/express'
    })
  },
  buyAgain: function (e){
    let index = e.currentTarget.dataset.index;
    wx.navigateTo({
      url: '/pages/product/product?id='+this.data.orderList[index].orderItemList[0].productId
    })
  }
})